package city_model;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class City {
	private List<Line> lines;
	private List<Station> stations;
	private List<Junction> junctions;
	
	
public City()
{
	lines = new ArrayList<Line>();
	stations = new ArrayList<Station>();
	junctions = new ArrayList<Junction>();
}

public void addLine(Line l)
{
	lines.add(l);
}

public void addStation(Station s)
{
	stations.add(s);
}

public boolean stationExists(String stationID)
{
	if (stations.isEmpty())
	{
		return false;
	}
	else
	{
		for (Station st:stations)
		{
			if (st.getId().equals(stationID))
			{
				return true;
			}
		}
	}
	return false;
}

public void addStationToJunctions(Station s)
{
	if (s.getJunctionName() != null)
	{
		boolean exists = false;
		
		if (!junctions.isEmpty())
		{
			for (Junction j:junctions)
			{
				if (j.getName().equals(s.getJunctionName()))
				{
					exists = true;
					j.addStation(s.getId());
				}
			}
		}	
		if (!exists || junctions.isEmpty())
		{
			Junction junc = new Junction(s.getJunctionName());
			junc.addStation(s.getId());
			junctions.add(junc);
		}
	}
}

public List<Junction> getJunctions()
{
	return junctions;
}

public List<Station> getStations()
{
	return stations;
}

public Station getStationById(String id)
{
	for (Station s:stations)
	{
		if (s.getId().equals(id))
		{
			return s;
		}
	}
	return null;
}

public ArrayList<Station> getStationsInWalkingDistance(Station currStation, int maximumWalkingDistance)
{
	ArrayList<Station> result = new ArrayList<Station>();
	for(Station s:stations)
	{
		int walkingDistance = currStation.directDistanceTo(s);
		if (walkingDistance != -1 && walkingDistance != 0 && walkingDistance <= maximumWalkingDistance)
		{
			result.add(s);
		}
	}
	return result;
}

public Junction getJunctionByStation(Station station)
{
	for (Junction j: junctions)
	{
		for (String s:j.getStations())
		{
			if (station.getId().equals(s))
			{
				return j;
			}
		}
	}
	return null;
}

public List<Line> getLinesForStation(Station s)
{
	ArrayList<Line> result = new ArrayList<Line>();
	for (Line l: lines)
	{
		if (l.getStationsForStation(s) != null)
		{
			result.add(l);
		}
	}
	if (result.size() == 0)
	{
		return null;
	}
	else
	{
		return result;
	}
}

public List<String> getNextStationForEachLine(Station station)
{
    ArrayList<String>  result = new ArrayList<String>();
    List<Line> lines = getLinesForStation(station);
    if (lines != null)
    {
    	for (Line l:lines)
    	{
    		for(Iterator<String> it = l.getStationsForStation(station).iterator(); it.hasNext();)
    		{
    			String currSt = it.next();
    			if (currSt.equals(station.getId()))
    			{
    				if(it.hasNext())
    				{
    					String nextSt = it.next();
    					if (!result.contains(nextSt))
    					{
    						result.add(nextSt);
    					}
    				}
    			}
    		}
    	}
    }
    if (result.size() == 0)
    {
    	return null;
    }
    else
    {
    	return result;
    }
    
}

public Line getLineById(String statioId)
{
	for (Line l : lines)
	{
		if (l.getId().equals(statioId))
		{
			return l;
		}
	}
	return null;
}

//public boolean isStationInStations(Station station)
//{
//	boolean stationPresent = false;
//	for(Station s:stations)
//	{
//		if (s.getId().equals(station.getId()))
//		{
//			stationPresent = true;
//		}
//	}
//	return stationPresent;
//}

}
